<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

        $dbTableList = Db::query("SHOW TABLE STATUS");
        $addonList = get_addon_list();
        $totalworkingaddon = 0;
        $totaladdon = count($addonList);
        foreach ($addonList as $index => $item) {
            if ($item['state']) {
                $totalworkingaddon += 1;
            }
        }
        //新增字段
        $totalshop = db('shop')->where([])->count();//总门店数
        $totalmerchant = db('merchant')->where([])->count();//总商家数
        $totalbaoorder = db('shopbaoorder')->where(['status'=>['in','2,3,4,6']])->count();//总包间订单
        $totalbaoordermoney = db('shopbaoorder')->where(['status'=>['in','2,3,4,6']])->sum('total');//总包间订单金额
        $totaljiuorder = db('shopjiuorder')->where(['status'=>['in','2,3']])->count();//总酒水订单
        $totaljiuordermoney = db('shopjiuorder')->where(['status'=>['in','2,3']])->sum('total');//总酒水订单金额
        $totaltopup = db('shoptopuporder')->where(['status'=>['in','2']])->count();//总充值订单
        $totaltopupmoney = db('shoptopuporder')->where(['status'=>['in','2']])->sum('total');//总充值订单金额
        $totalcoupon = db('usercouponorder')->where(['status'=>['in','2']])->count();//总优惠券订单
        $totalcouponmoney = db('usercouponorder')->where(['status'=>['in','2']])->sum('total');//总优惠券订单金额
        //今日数据
        $todaybaoorder = db('shopbaoorder')->where(['status'=>['in','2,3,4,6']])->whereTime('createtime','d')->count();//今日包间订单
        $todaybaoordermoney = db('shopbaoorder')->where(['status'=>['in','2,3,4,6']])->whereTime('createtime','d')->sum('total');//今日包间订单金额
        $todaybaoordertuimoney = db('shopbaoorder')->where(['status'=>['in','2,3,4,6']])->whereTime('createtime','d')->sum('refundmoney');//今日包间订单退款金额
        $todayjiuorder = db('shopjiuorder')->where(['status'=>['in','2,3']])->whereTime('createtime','d')->count();//今日酒水订单
        $todayjiuordermoney = db('shopjiuorder')->where(['status'=>['in','2,3']])->whereTime('createtime','d')->sum('total');//今日酒水订单金额
        $todaytopup = db('shoptopuporder')->where(['status'=>['in','2']])->whereTime('createtime','d')->count();//今日充值订单数
        $todaytopupmoney = db('shoptopuporder')->where(['status'=>['in','2']])->whereTime('createtime','d')->sum('total');//今日充值订单金额
        $todaycoupon = db('usercouponorder')->where(['status'=>['in','2']])->whereTime('createtime','d')->count();//今日优惠券订单数
        $todaycouponmoney = db('usercouponorder')->where(['status'=>['in','2']])->whereTime('createtime','d')->sum('total');//今日优惠券订单金额
        //昨日数据

        $yestodaybaoorder = db('shopbaoorder')->where(['status'=>['in','2,3,4,6']])->whereTime('createtime','yesterday')->count();//今日包间订单
        $yestodaybaoordermoney = db('shopbaoorder')->where(['status'=>['in','2,3,4,6']])->whereTime('createtime','yesterday')->sum('total');//今日包间订单金额
        $yestodaybaoordertuimoney = db('shopbaoorder')->where(['status'=>['in','2,3,4,6']])->whereTime('createtime','yesterday')->sum('refundmoney');//今日包间订单退款金额
        $yestodayjiuorder = db('shopjiuorder')->where(['status'=>['in','2,3']])->whereTime('createtime','yesterday')->count();//今日酒水订单
        $yestodayjiuordermoney = db('shopjiuorder')->where(['status'=>['in','2,3']])->whereTime('createtime','yesterday')->sum('total');//今日酒水订单金额
        $yestodaytopup = db('shoptopuporder')->where(['status'=>['in','2']])->whereTime('createtime','yesterday')->count();//今日充值订单数
        $yestodaytopupmoney = db('shoptopuporder')->where(['status'=>['in','2']])->whereTime('createtime','yesterday')->sum('total');//今日充值订单金额
        $yestodaycoupon = db('usercouponorder')->where(['status'=>['in','2']])->whereTime('createtime','yesterday')->count();//今日优惠券订单数
        $yestodaycouponmoney = db('usercouponorder')->where(['status'=>['in','2']])->whereTime('createtime','yesterday')->sum('total');//今日优惠券订单金额
        $this->view->assign([
            'totaluser'         => User::count(),
            'totaladdon'        => $totaladdon,
            'totaladmin'        => Admin::count(),
            'totalcategory'     => \app\common\model\Category::count(),
            'todayusersignup'   => User::whereTime('jointime', 'today')->count(),
            'todayuserlogin'    => User::whereTime('logintime', 'today')->count(),
            'sevendau'          => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
            'thirtydau'         => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
            'threednu'          => User::whereTime('jointime', '-3 days')->count(),
            'sevendnu'          => User::whereTime('jointime', '-7 days')->count(),
            'dbtablenums'       => count($dbTableList),
            'dbsize'            => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),
            'totalworkingaddon' => $totalworkingaddon,
            'attachmentnums'    => Attachment::count(),
            'attachmentsize'    => Attachment::sum('filesize'),
            'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
            'totalshop'=>$totalshop,
            'totalmerchant'=>$totalmerchant,
            'totalbaoorder'=>$totalbaoorder,
            'totalbaoordermoney'=>$totalbaoordermoney,
            'totaljiuorder'=>$totaljiuorder,
            'totaljiuordermoney'=>$totaljiuordermoney,
            'totaltopup'=>$totaltopup,
            'totaltopupmoney'=>$totaltopupmoney,
            'totalcoupon'=>$totalcoupon,
            'totalcouponmoney'=>$totalcouponmoney,
            'todaybaoorder'=>$todaybaoorder,
            'todaybaoordermoney'=>$todaybaoordermoney,
            'todaybaoordertuimoney'=>$todaybaoordertuimoney,
            'todayjiuorder'=>$todayjiuorder,
            'todayjiuordermoney'=>$todayjiuordermoney,
            'todaytopup'=>$todaytopup,
            'todaytopupmoney'=>$todaytopupmoney,
            'todaycoupon'=>$todaycoupon,
            'todaycouponmoney'=>$todaycouponmoney,
            'yestodaybaoorder'=>$yestodaybaoorder,
            'yestodaybaoordermoney'=>$yestodaybaoordermoney,
            'yestodaybaoordertuimoney'=>$yestodaybaoordertuimoney,
            'yestodayjiuorder'=>$yestodayjiuorder,
            'yestodayjiuordermoney'=>$yestodayjiuordermoney,
            'yestodaytopup'=>$yestodaytopup,
            'yestodaytopupmoney'=>$yestodaytopupmoney,
            'yestodaycoupon'=>$yestodaycoupon,
            'yestodaycouponmoney'=>$yestodaycouponmoney,
        ]);

        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));

        return $this->view->fetch();
    }
    public function test(){
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            // dump($where);
            $num = db('shopuser')->where($where)->count();
            $list[] = ['name'=>'会员数','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->count();
            $list[] = ['name'=>'包间订单数','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->sum('total');
            $list[] = ['name'=>'包间订单总金额','num'=>$num];
            $num = db('shopjiuorder')->where($where)->where(['status'=>['in','2,3']])->count();
            $list[] = ['name'=>'商品订单数','num'=>$num];
            $num = db('shopjiuorder')->where($where)->where(['status'=>['in','2,3']])->sum('total');
            $list[] = ['name'=>'商品订单总金额','num'=>$num];
            $num = db('shoptopuporder')->where($where)->where(['status'=>2])->count();
            $list[] = ['name'=>'充值总订单数','num'=>$num];
            $num = db('shoptopuporder')->where($where)->where(['status'=>2])->sum('total');
            $list[] = ['name'=>'充值订单总金额','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['type'=>1,'paytype'=>['in','6,7,10,11']])->count();
            $list[] = ['name'=>'优惠券总订单数（不含抖音、美团）','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['type'=>1,'paytype'=>['in','6,7,10,11']])->sum('total');
            $list[] = ['name'=>'优惠券订单总金额（不含抖音、美团）','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['type'=>1,'paytype'=>['in','1,6,9,10']])->count();
            $list[] = ['name'=>'总微信支付笔数','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['type'=>1,'paytype'=>['in','1,6,9,10']])->sum('total');
            $list[] = ['name'=>'总微信支付金额','num'=>$num];

            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['type'=>2])->count();
            $list[] = ['name'=>'总抖音券订单数','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['type'=>2])->sum('total');
            $list[] = ['name'=>'总抖音券金额数','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['type'=>3])->count();
            $list[] = ['name'=>'总美团券订单数','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['type'=>3])->sum('total');
            $list[] = ['name'=>'总美团券金额数','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['paytype'=>12])->count();
            $list[] = ['name'=>'现金开台订单数','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['paytype'=>12])->sum('total');
            $list[] = ['name'=>'现金开台金额数','num'=>$num];

            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','6']])->count();
            $list[] = ['name'=>'退款订单数','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','6']])->sum('total');
            $list[] = ['name'=>'退款订单金额','num'=>$num];

            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->whereTime('createtime','d')->count();
            $list[] = ['name'=>'今日包间订单数','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->whereTime('createtime','d')->sum('total');
            $list[] = ['name'=>'今日包间订单总金额','num'=>$num];
            
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','6']])->whereTime('createtime','d')->sum('total');
            $list[] = ['name'=>'今日包间退款金额','num'=>$num];
            $num = db('shopjiuorder')->where($where)->where(['status'=>['in','2,3']])->whereTime('createtime','d')->count();
            $list[] = ['name'=>'今日商品订单数','num'=>$num];
            $num = db('shopjiuorder')->where($where)->where(['status'=>['in','2,3']])->whereTime('createtime','d')->sum('total');
            $list[] = ['name'=>'今日商品订单总金额','num'=>$num];

            $num = db('shoptopuporder')->where($where)->where(['status'=>2])->whereTime('createtime','d')->count();
            $list[] = ['name'=>'今日充值总订单数','num'=>$num];
            $num = db('shoptopuporder')->where($where)->where(['status'=>2])->whereTime('createtime','d')->sum('total');
            $list[] = ['name'=>'今日充值订单总金额','num'=>$num];

            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['type'=>1,'paytype'=>['in','6,7,10,11']])->whereTime('createtime','d')->count();
            $list[] = ['name'=>'今日优惠券总订单数','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['type'=>1,'paytype'=>['in','6,7,10,11']])->whereTime('createtime','d')->sum('total');
            $list[] = ['name'=>'今日优惠券订单总金额','num'=>$num];

            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['paytype'=>12])->whereTime('createtime','d')->count();
            $list[] = ['name'=>'今日现金开台订单数','num'=>$num];
            $num = db('shopbaoorder')->where($where)->where(['status'=>['in','2,3,4,6']])->where(['paytype'=>12])->whereTime('createtime','d')->sum('total');
            $list[] = ['name'=>'今日现金开台金额数','num'=>$num];

            $result = array("total" => count($list), "rows" => $list);
            return json($result);
        }
    }

}
